package com.bawei.lib_widget.button

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.widget.AppCompatButton

/**
 *作者：lxy
 *功能:
 *时间：2025/5/29
 */
class ScalableButton(context: Context,attrs: AttributeSet?,defStyle:Int):AppCompatButton(context,attrs,defStyle),View.OnTouchListener {
    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    companion object {
        private const val SCALE = 0.95f
    }

    init {
        setOnTouchListener(this)
    }
    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        return if (isClickable&&isFocusable){
            when(p1?.action){
                MotionEvent.ACTION_DOWN -> {
                    scaleX = SCALE
                    scaleY = SCALE
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    scaleX = 1f
                    scaleY = 1f
                }
            }
            when(p1?.actionMasked){
                MotionEvent.ACTION_CANCEL -> {
                    scaleX = 1f
                    scaleY = 1f
                }
            }
            false
        }else{
            super.onTouchEvent(p1)
        }
    }
}